package com.lanswon.qzsmk.controller.cardManageController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.service.cardManageService.CancelCardService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.util.CopyBean;
import com.lanswon.qzsmk.vo.AppAcVo;
import com.lanswon.qzsmk.vo.CancelCardInfo;
import com.lanswon.qzsmk.vo.DepositVo;
import com.lanswon.qzsmk.vo.VoucherVo;
/**
 * 注销Controller
 * @author guyw
 *
 */
@RequestMapping(value="/cancelCard")
@Controller
public class CancelCardController {
	
	@Autowired
	CancelCardService cc;
	
	@Autowired
	ParamManageService pm;
	
	
	/**
	 * 链接到注销查询页面
	 * @return
	 */
	@RequiresPermissions("cancelCard:index")
	@RequestMapping(value="/queryCancelCard",name="注销,cancelCard:index",method={RequestMethod.GET,RequestMethod.POST})
	public String queryCancelCard(){
		
		return "CancelCard";
	}
	
	/**
	 * 查询某张卡开了哪些应用
	 * @param citizenCardNo
	 * @return
	 */
	//@RequiresPermissions("cancelCard:getAppAndEp")
	@RequestMapping(value="/getAppAndEp",name="注销,cancelCard:getAppAndEp",method=RequestMethod.POST)
	@ResponseBody
	public List<AppAcVo> getAppAndEp(String citizenCardNo){
		List<AppAcVo> list=cc.queryAppFlagAndWalletType(citizenCardNo);
		return list;
	}
	
	/**
	 * 获取需要注销的详细信息
	 * @param cci
	 * @param pageCount
	 */
	@RequiresPermissions("cancelCard:getCancelCardInfo")
	@RequestMapping(value="/getCancelCardInfo",name="注销查询注销信息,cancelCard:getCancelCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<CancelCardInfo> getCancelCardInfo(CancelCardInfo cci,Integer pageNum,Integer numPerPage){
//		System.out.println(cci.getCurrentPage()+";"+cci.getNumPerPage()+";"+pageCount);
		DwzPage<CancelCardInfo> dPage =null;
//		if(cci.getNumPerPage()==null){//加载页面时执行
//			cci.setCurrentPage(1);
//			cci.setNumPerPage(20);
//		}else {
//			if(cci.getCurrentPage()<=1||pageCount==0){
//				cci.setCurrentPage(1);
//			}else if(cci.getCurrentPage()>=pageCount&&pageCount!=0){
//				cci.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		cci.setCurrentPage(pageNum);
		cci.setNumPerPage(numPerPage);
		cci.setStartLine((cci.getCurrentPage()-1)*cci.getNumPerPage());
		dPage=cc.queryCancelCard(cci);
		return dPage;
		
	}
	
	/**
	 * 跳转注销登记页面，获取详细信息
	 * @param cancelCard
	 * @param isRead
	 * @param balance
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("cancelCard:goCancelRegister")
	@RequestMapping(value="/goCancelRegister",name="注销获取注销登记页面信息,cancelCard:goCancelRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> goCancelRegister(String[]cancelCard,String isRead,HttpServletRequest request) throws Exception{
		System.out.println(isRead);
		for(String str:cancelCard){
			System.out.println(str);
		}
		CancelCardInfo cci=new CancelCardInfo();
		cci.setCitizenCardNo(cancelCard[1]);
		cci.setCardPName(cancelCard[2]);
		cci.setCardTypeName(cancelCard[3]);
		cci.setPaperTypeName(cancelCard[4]);
		cci.setPaperNo(cancelCard[5]);
		cci.setCustomerName(cancelCard[6]);
		cci.setIsRead(isRead);
		CancelCardInfo cancelCardInfo=cc.queryPersonalInfo(cci);
		cci.setCardQualityAssurance(cancelCardInfo.getCardQualityAssurance());
		cci.setExpectedRefundsDate(cancelCardInfo.getExpectedRefundsDate());
		Map<String, Object> cancelInfo=new HashMap<>();
		cancelInfo.put("cci", cci);
		request.getSession().setAttribute("cancelInfo", cancelInfo);
		Map<String, Object> map=new HashMap<>();
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "cancelCard/cancelRegister");//post请求
		return map;
	}
	
	/**
	 * 链接到注销登记页面
	 * @return
	 */
	@RequiresPermissions("cancelCard:cancelRegister")
	@RequestMapping(value="/cancelRegister",name="注销登记页面,cancelCard:cancelRegister",method=RequestMethod.POST)
	public String cancelRegister(){
		
		return "CancleRegister";
		
	}
	
	/**
	 * 登记页面获取session信息
	 * @param request
	 * @return
	 */
	@RequiresPermissions("cancelCard:getCancelInfo")
	@RequestMapping(value="/getCancelInfo",name="注销登记页面获取session信息,cancelCard:getCancelInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getCancelInfo(HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String, Object> map=(Map<String, Object>) request.getSession().getAttribute("cancelInfo");
		request.getSession().removeAttribute("cancelInfo");
		return map;
	}
	
	/**
	 * 提交注销登记信息
	 * @param cci
	 * @return
	 * @throws ParseException 
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("cancelCard:commitCancelCardInfo")
	@RequestMapping(value="/commitCancelCardInfo",name="注销登记信息提交,cancelCard:commitCancelCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitCancelCardInfo(CancelCardInfo cci,HttpServletRequest request) throws ParseException, ApplicationException{
		Map<String, Object> map=new HashMap<>();
		VoucherVo vv=cc.commitCancelCardInfo(cci);//凭证页面的对象
		System.out.println(vv);
		//map.put("statusCode", "200");
		//map.put("callbackType", "forward");
		//map.put("forwardUrl", "cancelCard/queryCancelCard");//post请求，改跳转凭证页面地址
		request.getSession().setAttribute("vo", vv);
		request.getSession().setAttribute("type", "logoffCardRegister");
        map.put("title", "注销登记凭证");
        map.put("rel", "cancel");
        map.put("statusCode", "200");
        map.put("url", "printCredentials/toHairpinPdfView");
        //20181204 hgl 增加以下4行
        map.put("max", "true");
        map.put("jumpToTile", "注销");
        map.put("jumpToTabid", "cancelCard");
        map.put("jumpToUrl", "cancelCard/queryCancelCard");
        
		return map;
		
	}
	
	@RequiresPermissions("cancelCard:goShowDetail")
	@RequestMapping(value="/goShowDetail",name="去费用明细页面,cancelCard:goShowDetail",method=RequestMethod.GET)
	public String goShowDetail(DepositVo depositVo,HttpServletRequest request) throws Exception{
		depositVo.setCardPName(new String(depositVo.getCardPName().getBytes("iso8859-1"), "utf-8"));
		DepositVo devo=new DepositVo();
		devo=cc.queryNominalfee(depositVo);
		CopyBean.copyA2b(depositVo, devo);
		List<DepositVo>list=cc.queryDeposit(devo);
		Map<String, Object>map=new HashMap<>();
		map.put("nominalfee", devo);
		map.put("deposit", list);
		request.getSession().setAttribute("map", map);
		return "DeposiDetails";
	}
	
	@SuppressWarnings("unchecked")
	@RequiresPermissions("cancelCard:getShowDetail")
	@RequestMapping(value="/getShowDetail",name="获取费用明细信息,cancelCard:getShowDetail",method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> getShowDetail(HttpServletRequest request){
		Map<String, Object> map=null;
		map=(Map<String, Object>) request.getSession().getAttribute("map");
		request.getSession().removeAttribute("map");
		return map;
	}
}
